/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.services.acl;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.service.services.acl.AclsService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;
import opiam.admin.faare.unittests.javabeans.PersonTest;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;


/**
 * This class allows to test the initialization of the AclsService.
 */
public class AclsServiceTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(AclsServiceTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    // USER DATA

    /** Test user DN. */
    private static final String DN_USER = "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /** Test user DN. */
    private static final String DN_USERTEST = "uid=tclow, ou=People, dc=mycompany,dc=com";

    /**
     * Creates a new AclsServiceTest object.
     *
     * @param param  Parameter.
     */
    public AclsServiceTest(String param)
    {
        super(param);

        try
        {
            PropertiesManager.getInstance();

            if (!_USERCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("AclsserviceTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new AclsServiceTest("testBegin"));

        // TESTS SET
        suite.addTest(new AclsServiceTest("testInitialization"));
        suite.addTest(new AclsServiceTest("testInitializationWithDirectory"));

        suite.addTest(new AclsServiceTest("testEnd"));

        return (suite);
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        TestResult.getInstance().setInitiator("AclsServiceTest");
        _test.displayHeaderClass("AclsServiceTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);
        _test.displayTestStatement("AclsServiceTest");
    }

    /**
     * Tests the initialization without the directory parameter.
     * So, the NoControlAclPlugin class is used.
     *
     */
    public static void testInitialization()
    {
        _test.displayHeaderMethod("testInitialization");

        StringBuffer msg = new StringBuffer();

        try
        {
            PersonTest entry = new PersonTest();
            entry.setDn(DN_USERTEST);

            AclsService.initialize(null);

            boolean testOk = true;

            if (!AclsService.isServiceEnabled())
            {
                testOk = false;
                msg.append("Service is disabled\n");
            }
            else
            {
                msg.append("Service is enabled\n");
            }

            if (!AclsService.isCreationEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Creation is disabled\n");
            }
            else
            {
                msg.append("Creation is enabled\n");
            }

            if (!AclsService.isDeletionEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Deletion is disabled\n");
            }
            else
            {
                msg.append("Deletion is enabled\n");
            }

            if (!AclsService.isModificationEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Modification is disabled\n");
            }
            else
            {
                msg.append("Modification is enabled\n");
            }

            if (!AclsService.isVisualisationEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Visualisation is disabled\n");
            }
            else
            {
                msg.append("Visualisation is enabled\n");
            }

            if (testOk)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(msg.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msgEx = new StringBuffer();
            msgEx.append("Exception : ");
            msgEx.append(TestUtils.NEXT);
            msgEx.append(ex.toString());
            msgEx.append(TestUtils.NEXT);
            msgEx.append(msg.toString());
            _test.displayResultError(msgEx.toString());
        }

        _test.displayFooterMethod("testInitialization");
    }

    /**
     * Tests the initialization with the directory parameter.
     *
     */
    public static void testInitializationWithDirectory()
    {
        _test.displayHeaderMethod("testInitializationWithDirectory");

        StringBuffer msg = new StringBuffer();

        try
        {
            PersonTest entry = new PersonTest();
            entry.setDn(DN_USERTEST);

            //AclsService.initialize("opiam.admin.faare.unittests.service.services.acl");
            AclsService.initialize((AclsService.class.getResource(
                    "/config/service/acl")).getPath());

            boolean testOk = true;

            if (!AclsService.isServiceEnabled())
            {
                testOk = false;
                msg.append("Service is disabled\n");
            }
            else
            {
                msg.append("Service is enabled\n");
            }

            if (!AclsService.isCreationEnabled(entry, _USERCONTEXT))
            {
                msg.append("Creation is disabled\n");
            }
            else
            {
                testOk = false;
                msg.append("Creation is enabled\n");
            }

            if (AclsService.isDeletionEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Deletion is enabled\n");
            }
            else
            {
                msg.append("Deletion is disabled\n");
            }

            if (!AclsService.isModificationEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Modification is disabled\n");
            }
            else
            {
                msg.append("Modification is enabled\n");
            }

            if (!AclsService.isVisualisationEnabled(entry, _USERCONTEXT))
            {
                testOk = false;
                msg.append("Visualisation is disabled\n");
            }
            else
            {
                msg.append("Visualisation is enabled\n");
            }

            if (testOk)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(msg.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msgEx = new StringBuffer();
            msgEx.append("Exception : ");
            msgEx.append(TestUtils.NEXT);
            msgEx.append(ex.toString());
            msgEx.append(TestUtils.NEXT);
            msgEx.append(msg.toString());
            _test.displayResultError(msgEx.toString());
        }

        _test.displayFooterMethod("testInitializationWithDirectory");
    }
}
